<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">我寄的快递</block>
		</cu-custom>
		<text v-if="mySendArr.length==0">暂无数据</text>
		<block else v-for="item in mySendArr">
			<view class="card">
				<view>
					<view>
						快递公司：{{item.kuaidicom}}
					</view>
					<view>
						快递单号：{{item.kuaidinum}}
					</view>
					<view>
						快递状态：{{item.data.status}}
					</view>
					<view>
						快递员姓名：{{item.data.courierName}}
					</view>
					<view>
						快递员手机号：{{item.data.courierMobile}}
					</view>
				</view>
				<view style="display: flex;justify-content: space-around;margin-top:20upx;">
					<button type="warn" size="mini" @click="cancel(item.taskId,item.data.orderId)"
						:disabled="item.data.status==='用户主动取消'?true:false">{{item.data.status=="用户主动取消"?"已取消":"取消寄件"}}</button>
					<button type="warn" size="mini" @click="del(item._id)">删除订单</button>
				</view>
			</view>
		</block>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				//自己寄的所有快递数组
				mySendArr: [],
			};
		},
		onLoad() {
			this.getExpress()
		},
		async onPullDownRefresh() {
			await this.getExpress()
			uni.stopPullDownRefresh()
		},
		methods: {
			//获取数据库中的寄件数据
			getExpress() {
				return new Promise((resolve, reject) => {
					uniCloud.callFunction({
						name: 'getMySend',
						data: {
							userId: this.$store.state.userInfo._id
						},
						success: (res) => {
							this.mySendArr = res.result.data;
							resolve(res)
						},
						fail:(res) => {
							console.log(res);
							reject(res)
						}
					})
				})
			},
			//取消寄快递
			cancel(tId, oId) {
				uni.request({
					url: "http://cloud.kuaidi100.com/api",
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					data: {
						secret_key: "znInVfCiSFkpBrw7dj",
						secret_code: "bcb0428c1dd84aca9d6e19ea4dcf683d",
						secret_sign: "1E717DFE67B4C50B13C6CF2B61EE6813",
						orderId: oId,
						taskId: tId,
						cancelMsg: "程序员测试功能"
					},
					success: (res) => {
						uni.showToast({
							title: '取消寄件成功',
							icon: 'success'
						});
						this.getExpress()
					}
				})
			},
			//删除订单历史,其实就是删除数据库中的这条记录
			del(e) {
				uni.showModal({
					title: '确认要删除吗？',
					success: (res) => {
						if (res.confirm) {
							console.log(e);
							uniCloud.callFunction({
								name: 'delMySend',
								data: {
									id: e
								},
								success: (res) => {
									console.log(res);
									//删除完成后要刷新页面
									this.getExpress()
								}
							})
						} else if (res.cancel) {
							return;
						}

					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f1f0f0 !important;
	}

	.card {
		background-color: #FFFFFF;
		padding: 20upx;
		margin: 20upx 10upx;
		border-radius: 10upx;
		font-size: 30upx;
		line-height: 45upx;
	}
</style>
